package org.example.myleet.rosalind.asmq;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Solution {
    /**
     * [Rosalind]【倒序排序】Assessing Assembly Quality with N50 and N75
     * https://rosalind.info/problems/asmq/
     * 题目的意思是找出最短的序列长度L，使得大于等于L的序列加起来的长度刚好大于等于总长度的50%或75%
     * 因此只要先倒序排一下，然后逐个序列长度加起来，刚好超过XX%的时候输出那个序列的长度即可
     */
    public static void main(String[] args) throws IOException {
        Solution solution = new Solution();
        String path = solution.getClass().getResource("").getPath() + "example.txt";
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
        List<String> reads = new ArrayList<>(1000);
        String line;
        while (null != (line = br.readLine())) {
            if (line.length() < 1) {
                break;
            }
            reads.add(line);
        }
        int totalLen = 0;
        List<Integer> readLengths = new ArrayList<>(reads.size());
        for (String read : reads) {
            totalLen += read.length();
            readLengths.add(read.length());
        }
        readLengths.sort(Comparator.reverseOrder());
        System.out.println(calNXX(readLengths, (int) Math.ceil(totalLen * 0.5)));
        System.out.println(calNXX(readLengths, (int) Math.ceil(totalLen * 0.75)));
    }
    private static int calNXX(List<Integer> readLengths, int target) {
        int sum = 0;
        for (int len : readLengths) {
            sum += len;
            if (sum >= target) {
                return len;
            }
        }
        return -1;
    }
}
